<template>
    <f7-page @page:beforein="pageBeforeIn">
        <f7-navbar>
            <f7-nav-left>
                <f7-link @click="back">
                    <f7-icon icon="icon-back"></f7-icon>
                </f7-link>
            </f7-nav-left>
            <f7-nav-title title="实测实量"></f7-nav-title>
			<f7-nav-right>
                <f7-link @click="summary">汇总</f7-link>
            </f7-nav-right>
        </f7-navbar>
        <f7-block strong style="margin-top:0; margin-bottom:0; padding-top:3px; padding-bottom:3px;">
            <f7-block-title style="margin-top:5px;">
                <f7-row style="color:#007aff;">
                    <f7-col width="50" @click="placeCascaderOpen">
                        <p style="font-size:14px !important; margin-top:0; margin-bottom:0;">
                            {{bidSectionId && $store.state.global.bidSectionMap[bidSectionId].bid_section_name}}
                        </p>
                        <p style="font-size:14px !important; margin-top:0; margin-bottom:0;">
                            {{(buildingId && $store.state.global.buildingMap[buildingId].building_name) + ' - ' + 
                            (unitId && $store.state.global.unitMap[unitId].unit_name) + '单元'}}
                        </p>
                    </f7-col>
                    <f7-col width="50" @click="checkItemCascaderOpen">
                        <p style="font-size:14px !important; margin-top:0; margin-bottom:0;">
                            {{parentProblemClassId && $store.state.global.problemClassMap[parentProblemClassId].problem_class_name}}
                        </p>
                        <p style="font-size:14px !important; margin-top:0; margin-bottom:0;">
                            {{
                            (problemClassId && $store.state.global.problemClassMap[problemClassId].problem_class_name) + 
                            ($store.state.global.checkItemGuidelineMap[problemClassId] && 
                                ('-' + processModeText($store.state.global.checkItemGuidelineMap[problemClassId].process_mode))
                            )
                            }}
                        </p>
                    </f7-col>
                </f7-row>
            </f7-block-title>
        </f7-block>
        <f7-row style="margin-left:10px; margin-right:10px; margin-top: 10px; text-align:center;">
            <f7-col v-show="hasPermissionContractor">
                <f7-badge color="green">施工单位已测</f7-badge>
                <br/>{{measureCountAll == 0 ? 0 : measureCountContractor}}%
            </f7-col>
            <f7-col v-show="hasPermissionSupervisor">
                <f7-badge color="orange">监理单位已测</f7-badge>
                <br/>{{measureCountAll == 0 ? 0 : measureCountSupervisor}}%
            </f7-col>
            <f7-col v-show="hasPermissionConstructor">
                <f7-badge color="blue">建设单位已测</f7-badge>
                <br/>{{measureCountAll == 0 ? 0 : measureCountConstructor}}%
            </f7-col>
        </f7-row>
        <f7-block inset style="margin-top:0; margin-bottom:0;"
            v-if="$store.state.global.checkItemGuidelineMap[problemClassId] && $store.state.global.checkItemGuidelineMap[problemClassId].process_mode != '1'">
            <f7-block-title style="margin-top:5px; margin-bottom:5px;">楼层</f7-block-title>
            <f7-block strong style="margin-top:0; margin-bottom:0;">
                <f7-row v-for="(floor, i) in floors" v-if="i % 3 == 0" :key="floor.id" 
                    style="margin-bottom:5px;">
                    <f7-col v-for="j in 3" v-if="j - 1 + i < floors.length" 
                        :key="floors[j - 1 + i].id">
                        <f7-button :fill="isMeasured(unitId + '_' + floors[j - 1 + i].id)"
                            :color="measuredColor(unitId + '_' + floors[j - 1 + i].id)"
                            @click="houseDetail(floors[j - 1 + i].id, null, problemClassId)">
                            {{floors[j - 1 + i].name}}层
	                        <div v-if="hasDraft(unitId + '_' + floors[j - 1 + i].id) || hasModify(unitId + '_' + floors[j - 1 + i].id)" 
	                            style="position:absolute; width:10px; height:10px; border-radius:5px; left:0px; top:0px; background-color:red;">
	                        </div>
                        </f7-button>
                    </f7-col>
                </f7-row>
            </f7-block>
        </f7-block>
        <f7-block v-else inset v-for="(floor, i) in floors" :key="floor.id" style="margin-top:0; margin-bottom:0;">
            <f7-block-title style="margin-top:5px; margin-bottom:5px;">{{floor.name}}</f7-block-title>
            <f7-block strong style="margin-top:0; margin-bottom:0;">
                <f7-row v-for="(house, j) in floor.houses" v-if="j % 3 == 0" :key="house.id" style="margin-bottom:5px;">
                    <f7-col v-for="k in 3" v-if="k - 1 + j < floor.houses.length" :key="floor.houses[k - 1 + j].id">
                        <f7-button :fill="isMeasured(floor.houses[k - 1 + j].id)"
                            :color="measuredColor(floor.houses[k - 1 + j].id)"
                            @click="houseDetail(floor.id, floor.houses[k - 1 + j].id, problemClassId)">
                            {{floor.name}}-{{floor.houses[k - 1 + j].name}}
                            <div v-if="hasDraft(floor.houses[k - 1 + j].id) || hasModify(floor.houses[k - 1 + j].id)" 
                                style="position:absolute; width:10px; height:10px; border-radius:5px; left:0px; top:0px; background-color:red;">
                            </div>
                        </f7-button>
                    </f7-col>
                </f7-row>
            </f7-block>
        </f7-block>
        <SheetCascader ref="placeCascader" :colAmount="3" :colTrees="placeTrees"
            :colsWidth="[50, 20, 30]" :colsValue="[bidSectionId, buildingId, unitId]"
            @selected="placeSelected">
        </SheetCascader>
        <SheetCascader ref="checkItemCascader" :colAmount="2" :colTrees="checkItemTrees"
            :colsValue="[parentProblemClassId, problemClassId]" @selected="checkItemSelected">
        </SheetCascader>
        
    </f7-page>
</template>

<script>
import back from '@/mixins/back.js';
import Common from "@/utils/Common.js";
import DateUtil from "@/utils/DateUtil.js";
import $ from 'jquery';
import events from 'events';
import store from '@/store';
import keys from '@/utils/rememberKeys';
import { getUser, setUser, clearUser } from '@/utils/auth';
import { getStore, setStore, clearStore } from '@/utils/storage';
import request from '@/utils/request';
import db from '@/utils/db';
import SheetCascader from "@/views/common/SheetCascader.vue";

export default {
    mixins: [back],
	data: function() {
        return {
        	placeTrees: [],
        	checkItemTrees: [],
        	bidSectionId: null,
        	buildingId: null,
        	unitId: null,
        	parentProblemClassId: null,
        	problemClassId: null,
        	qualityMeasureMap: {},
        	floors: [],
        	placeColAmount: 0,
        	checkItemColAmount: 0
        };
    },
    components: {
    	SheetCascader
    },
	methods: {
		pageBeforeIn: function() {
			this.unitId = getStore(keys.qualityMeasureUnitIdKey);
	        this.problemClassId = getStore(keys.qualityMeasureProblemClassIdKey);
	        
	        var projectId = getStore(keys.projectIdKey);
	    	var bidSectionMap = {};
	    	var buildingMap = {};
	    	var unitMap = {};
	    	var placeTrees = [];
	    	let sql = "select * from tb_unit where building_id in (select id from tb_building where project_id = ? )";
	    	Common.executeSql(sql, [projectId]).then((result) => {
	    		let rs = result.rs;
	    		for(let i = 0; i < rs.rows.length; i++) {
	    			let unit = rs.rows.item(i);
	        		var unitTree = {
	        			value: unit.id,
	        			displayValue: unit.unit_name + '单元',
	        			children: []
	        		};
	        		unitMap[unitTree.value] = unitTree;
	        		var buildingTree = buildingMap[unit.building_id];
	        		if(buildingTree == null) {
	        			var building = this.$store.state.global.buildingMap[unit.building_id];
	        			buildingTree = {
	        				value: building.id,
	        				displayValue: building.building_name,
	        				children: []
	        			};
	        			buildingMap[buildingTree.value] = buildingTree;
	        			var bidSectionTree = bidSectionMap[building.bid_section_id];
	        			if(bidSectionTree == null) {
	        				var bidSection = this.$store.state.global.bidSectionMap[building.bid_section_id];
	        				if(bidSection == null || undefined == bidSection){
	        					continue;
	        				}
	        				if(projectId !== bidSection.project_id){
	        					continue;
	        				}
	        				var bidSectionTree = {
	        					value: bidSection.id,
	        					displayValue: bidSection.bid_section_name,
	        					children: []
	        				};
	        				bidSectionMap[bidSectionTree.value] = bidSectionTree;
	        				placeTrees.push(bidSectionTree);
	        			}
	        			bidSectionTree.children.push(buildingTree);
	        		}
	        		buildingTree.children.push(unitTree);
	    		}
	    		this.placeTrees = placeTrees;
	        	if(this.unitId == null) {
	        		this.bidSectionId = placeTrees[0].value;
	        		this.buildingId = placeTrees[0].children[0].value;
	        		this.unitId = placeTrees[0].children[0].children[0].value;
	        	} else {
	        		this.buildingId = this.$store.state.global.unitMap[this.unitId].building_id;
	        		this.bidSectionId = this.$store.state.global.buildingMap[this.buildingId].bid_section_id;
	        	}
	        	
	        	var problemClasses = [];
	            var problemClassIds = this.$store.state.global.bidSectionCheckItemRel.bidSectionMap[this.bidSectionId];
	            for(var itemId in problemClassIds){
	            	var problemClass = this.$store.state.global.problemClassMap[
	            		this.$store.state.global.bidSectionCheckItemRel[problemClassIds[itemId]].check_item_id
	            	];
	                if(problemClass.type == '41') {
	                	if(!problemClasses.includes(problemClass)){
	                		problemClasses.push(problemClass);
	                	}
	                }
	            }
	            problemClasses = problemClasses.sort(function(pc1, pc2) {
	                if(pc1.level < pc2.level) {
	                    return -1;
	                } else if(pc1.level == pc2.level) {
	                    return 0;
	                } else {
	                    return 1;
	                }
	            });
	            var checkItemTrees = [];
	            var checkItemTreeMap = {};
	            for(var i = 0; i < problemClasses.length; i++) {
	                var problemClass = problemClasses[i];
	                var checkItemTree = {
	                    value: problemClass.id,
	                    displayValue: problemClass.problem_class_name,
	                    children: []
	                };
	                checkItemTreeMap[checkItemTree.value] = checkItemTree;
	                if(problemClass.level == 2) {
	                    checkItemTrees.push(checkItemTree);
	                } else {
	                    var parentCheckItemTree = checkItemTreeMap[problemClass.parent_problem_class_id];
	                    var guideline = this.$store.state.global.checkItemGuidelineMap[checkItemTree.value];
	                    if(guideline != null) {
	                    	checkItemTree.displayValue += '-' + this.processModeText(guideline.process_mode);
	                    }
	                    parentCheckItemTree.children.push(checkItemTree);
	                }
	            }
	            if(checkItemTreeMap[this.problemClassId] == null) {
	                this.parentProblemClassId = checkItemTrees[0].value;
	                this.problemClassId = checkItemTrees[0].children[0].value;
	            } else {
	            	this.parentProblemClassId = 
	            		this.$store.state.global.problemClassMap[this.problemClassId].parent_problem_class_id;
	            }
	            this.checkItemTrees = checkItemTrees;
	            
	            setStore(keys.qualityMeasureUnitIdKey,this.unitId);
	            setStore(keys.qualityMeasureProblemClassIdKey,this.problemClassId);
	            this.placeColAmount = 3;
	        	this.checkItemColAmount = 2;
	            this.reloadDraft();
	            this.reloadModify();
	    	}).catch(error => {
				console.error(error);
			});
		},
		summary: function() {
        	var url = "/WorkbenchQualityMeasureProblemSummary";
        	var router = this.$f7router.navigate(url, {
        		props: {
        			unitId: this.unitId,
        			itemId: this.problemClassId
        		}
        	});
        },
        isMeasured: function(key) {
        	var qualityMeasure = this.qualityMeasureMap[key];
        	if(qualityMeasure != null) {
        		if(this.hasPermissionConstructor) {
        			if(qualityMeasure['建设单位'] != null || 
        			    qualityMeasure['监理单位'] != null || 
                        qualityMeasure['施工单位'] != null) {
                        return true;
        			}
        		} else if(this.hasPermissionSupervisor) {
        			if(qualityMeasure['监理单位'] != null || 
                        qualityMeasure['施工单位'] != null) {
                        return true;
                    }
        		} else if(this.hasPermissionContractor) {
        			if(qualityMeasure['施工单位'] != null) {
                        return true;
                    }
        		}
        	}
        	return false;
        },
        measuredColor: function(key) {
        	if(!this.isMeasured(key)) {
        		return 'blue';
        	}
            var qualityMeasure = this.qualityMeasureMap[key];
            if(qualityMeasure != null) {
                if(qualityMeasure['建设单位'] != null && this.hasPermissionConstructor) {
                    return 'blue';
                }
                if(qualityMeasure['监理单位'] != null && this.hasPermissionSupervisor) {
                    return 'orange';
                }
                if(qualityMeasure['施工单位'] != null && this.hasPermissionContractor) {
                    return 'green';
                }
            }
            return 'gray';
        },
        placeCascaderOpen: function() {
    		this.$refs.placeCascader.open();
    	},
    	placeSelected: function(values) {
    		this.bidSectionId = values[0];
    		this.buildingId = values[1];
    		this.unitId = values[2];
    	},
    	checkItemCascaderOpen: function() {
    		var treeMap = this.$refs.checkItemCascader.treeMap;
    		var checkItemRateMap = this.$store.state.global.unitCheckItemRateMap[this.unitId];
    		if(checkItemRateMap == null) {
    			checkItemRateMap = {};
    		}
    		for(var itemId in treeMap) {
    			var displayValue = treeMap[itemId].displayValue;
    			displayValue = displayValue.substring(displayValue.indexOf('% ')+1,displayValue.length);
    			var rate = checkItemRateMap[itemId];
    			if(rate != null) {
    				displayValue = rate + "% " + displayValue;
    			}
    			treeMap[itemId].displayValue = displayValue;
    		}
            this.$refs.checkItemCascader.open();
    	},
    	checkItemSelected: function(values) {
            this.parentProblemClassId = values[0];
            this.problemClassId = values[1];
        },
        houseDetail: function(floorId, houseId, problemClassId) {
        	var self = this;
        	var url = '/WorkbenchQualityMeasureHouseDetail?';
        	url += "itemId=" + problemClassId;
        	url += "&bidSectionId=" + this.bidSectionId;
        	url += "&buildingId=" + this.buildingId;
            url += "&unitId=" + this.unitId;
        	url += "&floorId=" + floorId;
        	if(houseId != null) {
                url += "&houseId=" + houseId;
        	}
        	var router = this.$f7router.navigate(url);
            router.off('submitedProblem');
            router.on('submitedProblem', function(problem) {
            	var checkItemGuideline = self.$store.state.global.checkItemGuidelineMap[problem.item_id];
            	var key = problem.room_id;
            	if(checkItemGuideline) {
            		if(checkItemGuideline.process_mode != '1') {
            			key = problem.unit_id + '_' + problem.floor_id;
            		}
            	}
            	var qualityMeasure = self.qualityMeasureMap[key];
            	if(qualityMeasure == null) {
            		qualityMeasure = {
            			'施工单位': null,
            			'监理单位': null,
            			'建设单位': null
            		}
            		self.$set(self.qualityMeasureMap, key, qualityMeasure);
            	}
            	self.$set(qualityMeasure, problem.app_permission, problem);
            });
        },
        reloadFloors: function() {
        	var self = this;
            var floorMap = {};
            var floors = [];
            for(var houseId in self.$store.state.global.houseMap) {
                var house = self.$store.state.global.houseMap[houseId];
                if(houseId != "floorUnitMap" && house.unit_id == self.unitId) {
                    var floor = floorMap[house.floor_id];
                    if(floor == null) {
                        floor = {
                            id: house.floor_id,
                            name: self.$store.state.global.floorMap[house.floor_id].floor_name,
                            houses: []
                        }
                        floorMap[floor.id] = floor;
                        floors.push(floor);
                    }
                    floor.houses.push({
                        id: houseId,
                        name: house.house_name
                    });
                }
            }
            floors.sort(function(floor1, floor2) {
                var f1 = parseInt(floor1.name);
                if(isNaN(f1)) {
                	return 1;
                }
                var f2 = parseInt(floor2.name);
                if(isNaN(f2)) {
                	return -1;
                }
                if(f1 < f2) {
                    return -1;
                } else if(f1 == f2) {
                    return 0
                } else {
                    return 1;
                }
            });
            floors.forEach(function(floor) {
                floor.houses.sort(function(house1, house2) {
                    var h1 = parseInt(house1.name);
                    if(isNaN(h1)) {
                        return 1;
                    }
                    var h2 = parseInt(house2.name);
                    if(isNaN(h2)) {
                        return -1;
                    }
                    if(h1 < h2) {
                        return -1;
                    } else if(h1 == h2) {
                        return 0;
                    } else {
                        return 1;
                    }
                });
            });
            this.floors = floors;
        },
        downloadQualityMeasureCheckProblems: function() {
        	var self = this;
        	if(Common.isOnline()) {
        		self.$f7.preloader.show();
        		let sql = "delete from tb_quality_measure_checkquality_problem where item_id = ?";
                Common.executeSql(sql, [self.problemClassId]).then(() => {
                	return request({
                		url: '/rest/api/v3/qualityMeasureCheckQualityProblem/findByProjectIdAndItemId',
                        method: 'get',
                        params: {
                        	projectId: getStore(keys.projectIdKey),
                            itemId: self.problemClassId
                        }
                    });
        		}).then((data) => {
        			var elements = data.list;
        			if(elements == null){
        				elements = [];
        			}
        			return db.insert('tb_quality_measure_checkquality_problem', elements);
        		}).then(() => {
            		self.$f7.preloader.hide();
            		self.reloadQualityMeasureCheckProblems();
        		}).catch(error => {
        			console.error(error);
        			self.reloadQualityMeasureCheckProblems();
            		self.$f7.preloader.hide();
        		});
        	}
             
        },
        reloadQualityMeasureCheckProblems: function() {
        	var self = this;
            self.$f7.preloader.show();
        	var qualityMeasureMap = {};
        	let sql = "\
                select * from tb_quality_measure_checkquality_problem t \
                where t.item_id = '" + self.problemClassId + "' \
            ";
        	Common.executeSql(sql, []).then((result) => {
        		let rs = result.rs;
        		let checkItemGuideline = self.$store.state.global.checkItemGuidelineMap[self.problemClassId];
                for(var i = 0; i < rs.rows.length; i++) {
                    var qualityMeasureCheckQualityProblem = rs.rows.item(i);
                	if(checkItemGuideline == null || checkItemGuideline.process_mode == '1') {
                        var qualityMeasure = qualityMeasureMap[qualityMeasureCheckQualityProblem.room_id];
                        if(qualityMeasure == null) {
                            qualityMeasure = {
                                '施工单位': null,
                                '监理单位': null,
                                '建设单位': null
                            }
                            qualityMeasureMap[qualityMeasureCheckQualityProblem.room_id] =
                                qualityMeasure;
                        }
                        qualityMeasure[qualityMeasureCheckQualityProblem.app_permission] = 
                            qualityMeasureCheckQualityProblem;
                	} else if(checkItemGuideline.process_mode == '2' || checkItemGuideline.process_mode == '5') {
                            var qualityMeasure = qualityMeasureMap[qualityMeasureCheckQualityProblem.unit_id 
                            	+ '_' + qualityMeasureCheckQualityProblem.floor_id]; 
                            if(qualityMeasure == null) {
                                qualityMeasure = {
                                    '施工单位': null,
                                    '监理单位': null,
                                    '建设单位': null
                                }
                                qualityMeasureMap[qualityMeasureCheckQualityProblem.unit_id 
                                	+ '_' + qualityMeasureCheckQualityProblem.floor_id] =
                                    qualityMeasure;
                            }
                            qualityMeasure[qualityMeasureCheckQualityProblem.app_permission] = 
                                qualityMeasureCheckQualityProblem;
                	}
    			}
                self.qualityMeasureMap = qualityMeasureMap;
                self.$f7.preloader.hide();
        	}).catch(error => {
    			console.error(error);
    			this.$f7.preloader.hide();
    		});
        	
        },
        processModeText: function(processMode) {
            if(processMode == '1') {
                return '分户';
            } else if(processMode == '2') {
                return '不分单元整层';
            } else if(processMode == '5') {
                return '分单元整层';
            }
            return '';
        },
        hasDraft: function(key) {
        	var draft = this.$store.state.global.draftMap[key];
        	if(draft == null) {
        		return false;
        	}
        	var user = this.$store.state.global.userMap[getUser().userId];
        	if(draft.app_permission == user.app_permission) {
        		return true;
        	} else {
        		return false;
        	}
        },
        hasModify: function(key) {
        	var qualityMeasure = this.qualityMeasureMap[key];
        	if(qualityMeasure == null) {
        		return false;
        	}
            var user = this.$store.state.global.userMap[getUser().userId];
            var problem = qualityMeasure[user.app_permission];
            if(problem == null) {
            	return false;
            }
            if(this.$store.state.global.modifyMap[problem.quality_measure_checkquality_problem_id] != null) {
            	return true;
            } else {
            	return false;
            }
        },
        reloadDraft: function() {
        	var self = this;
        	self.$store.state.global.draftMap = {};
        	let sql = "\
                select * \
                from tmp_quality_measure_checkquality_problem_regist t \
                where t.item_id = '" + self.problemClassId + "' \
            ";
        	Common.executeSql(sql, []).then((result) => {
        		let rs = result.rs;
        		let checkItemGuideline = self.$store.state.global.checkItemGuidelineMap[self.problemClassId];
                for(var i = 0; i < rs.rows.length; i++) {
                    var row = rs.rows.item(i);
                    var key = row.house_id;
                    if(checkItemGuideline) {
                    	if(checkItemGuideline.process_mode != '1') {
                    		key = row.unit_id + '_' + row.floor_id;
                    	}
                    }
                    var draftProblem = self.$store.state.global.draftMap[key];
                    if(draftProblem == null) {
                        draftProblem = {
                            '施工单位': null,
                            '监理单位': null,
                            '建设单位': null
                        };
                        self.$set(self.$store.state.global.draftMap, key, row);
                    }
                    draftProblem[row.app_permission] = row;
                }
        	}).catch(error => {
    			console.error(error);
    		});
        },
        reloadModify: function() {
            var self = this;
            self.$store.state.global.modifyMap = {};
            let today = new Date();
            today = DateUtil.dateToStr('yyyyMMdd', today);
            today = DateUtil.strFormatToDate('yyyyMMdd', today);
            today = today.getTime();
            let sql = "\
                delete \
                from tmp_quality_measure_checkquality_problem_modify \
                where quality_measure_checkquality_problem_id in ( \
                    select quality_measure_checkquality_problem_id \
                    from tb_quality_measure_checkquality_problem \
                    where regist_date < " + today + " \
                ) \
            ";
            Common.executeSql(sql, []).then(() => {
            	let sql = "\
                    select * \
                    from tmp_quality_measure_checkquality_problem_modify t \
                ";
            	return Common.executeSql(sql, []);
            }).then((result) => {
            	let rs = result.rs;
            	for(let i = 0; i < rs.rows.length; i++) {
                    let row = rs.rows.item(i);
                    self.$set(self.$store.state.global.modifyMap, 
                        row.quality_measure_checkquality_problem_id, row);
                }
            }).catch(error => {
    			console.error(error);
    		});
        }
	},
	created: function() {
         
	},
	mounted: function() {
		var self = this;
    	self.$f7.preloader.show();
        this.$watch('bidSectionId', function(newVal, oldVal) {
            var problemClasses = [];
            var problemClassIds = this.$store.state.global.bidSectionCheckItemRel.bidSectionMap[this.bidSectionId];
            for(var itemId in problemClassIds){
            	var problemClass = this.$store.state.global.problemClassMap[
            		this.$store.state.global.bidSectionCheckItemRel[problemClassIds[itemId]].check_item_id
            	];
                if(problemClass.type == '41') {
                	if(!problemClasses.includes(problemClass)){
                		problemClasses.push(problemClass);
                	}
                }
            }
            problemClasses = problemClasses.sort(function(pc1, pc2) {
                if(pc1.level < pc2.level) {
                    return -1;
                } else if(pc1.level == pc2.level) {
                    return 0;
                } else {
                    return 1;
                }
            });
            var checkItemTrees = [];
            var checkItemTreeMap = {};
            for(var i = 0; i < problemClasses.length; i++) {
                var problemClass = problemClasses[i];
                var checkItemTree = {
                    value: problemClass.id,
                    displayValue: problemClass.problem_class_name,
                    children: []
                };
                checkItemTreeMap[checkItemTree.value] = checkItemTree;
                if(problemClass.level == 2) {
                    checkItemTrees.push(checkItemTree);
                } else {
                    var parentCheckItemTree = checkItemTreeMap[problemClass.parent_problem_class_id];
                    var guideline = this.$store.state.global.checkItemGuidelineMap[checkItemTree.value];
                    if(guideline != null) {
                        checkItemTree.displayValue += '-' + this.processModeText(guideline.process_mode);
                    }
                    parentCheckItemTree.children.push(checkItemTree);
                }
            }
            if(checkItemTreeMap[this.problemClassId] == null) {
                this.parentProblemClassId = checkItemTrees[0].value;
                this.problemClassId = checkItemTrees[0].children[0].value;
            } else {
                this.parentProblemClassId = 
                    this.$store.state.global.problemClassMap[this.problemClassId].parent_problem_class_id;
            }
            this.checkItemTrees = checkItemTrees;
        });
        this.$watch('buildingId', function(newVal, oldVal) {
            
        });
        this.$watch('unitId', function(newVal, oldVal) {
            setStore(keys.qualityMeasureUnitIdKey,this.unitId);
            this.reloadFloors();
        });
        this.$watch('parentProblemClassId', function(newVal, oldVal) {
            
        });
        this.$watch('problemClassId', function(newVal, oldVal) {
            setStore(keys.qualityMeasureProblemClassIdKey,this.problemClassId);
            this.downloadQualityMeasureCheckProblems();
            this.reloadDraft();
        });
        self.reloadFloors();
    	self.downloadQualityMeasureCheckProblems();
	},
	computed: {
    	hasPermissionContractor: function() {
    		var userId = getUser().userId;
    		var user = this.$store.state.global.userMap[userId];
    		if(user.app_permission == '施工单位' || user.app_permission == '监理单位' || user.app_permission == '建设单位') {
    			return true;
    		} else {
    			return false;
    		}
    	},
        hasPermissionSupervisor: function() {
            var userId = getUser().userId;
            var user = this.$store.state.global.userMap[userId];
            if(user.app_permission == '监理单位' || user.app_permission == '建设单位') {
                return true;
            } else {
                return false;
            }
        },
        hasPermissionConstructor: function() {
            var userId = getUser().userId;
            var user = this.$store.state.global.userMap[userId];
            if(user.app_permission == '建设单位') {
                return true;
            } else {
                return false;
            }
        },
        measureCountContractor: function() {
        	var self = this;
            var checkItemGuideline = this.$store.state.global.checkItemGuidelineMap[this.problemClassId];
            var total = this.measureCountAll;
            var measure = 0;
            if(total == 0) {
                return 0;
            } else {
                this.floors.forEach(function(floor) {
                    if(checkItemGuideline == null || checkItemGuideline.process_mode == '1') {
                        floor.houses.forEach(function(house) {
                            var qualityMeasure = self.qualityMeasureMap[house.id];
                            if(qualityMeasure != null) {
                                if(qualityMeasure['施工单位'] != null) {
                                    measure++;
                                }
                            }
                        });
                    } else {
                        var qualityMeasure = self.qualityMeasureMap[self.unitId + '_' + floor.id];
                        if(qualityMeasure != null) {
                            if(qualityMeasure['施工单位'] != null) {
                                measure++;
                            }
                        }
                    }
                });
                var result = measure / total * 100;
                result = result.toFixed(1);
                return result;
            }
        },
        measureCountSupervisor: function() {
            var self = this;
            var checkItemGuideline = this.$store.state.global.checkItemGuidelineMap[this.problemClassId];
            var total = this.measureCountAll;
            var measure = 0;
            if(total == 0) {
                return 0;
            } else {
                this.floors.forEach(function(floor) {
                    if(checkItemGuideline == null || checkItemGuideline.process_mode == '1') {
                        floor.houses.forEach(function(house) {
                            var qualityMeasure = self.qualityMeasureMap[house.id];
                            if(qualityMeasure != null) {
                                if(qualityMeasure['监理单位'] != null) {
                                    measure++;
                                }
                            }
                        });
                    } else {
                        var qualityMeasure = self.qualityMeasureMap[self.unitId + '_' + floor.id];
                        if(qualityMeasure != null) {
                            if(qualityMeasure['监理单位'] != null) {
                                measure++;
                            }
                        }
                    }
                });
                var result = measure / total * 100;
                result = result.toFixed(1);
                return result;
            }
        },
        measureCountConstructor: function() {
            var self = this;
            var checkItemGuideline = this.$store.state.global.checkItemGuidelineMap[this.problemClassId];
        	var total = this.measureCountAll;
        	var measure = 0;
        	if(total == 0) {
        		return 0;
        	} else {
        		this.floors.forEach(function(floor) {
                    if(checkItemGuideline == null || checkItemGuideline.process_mode == '1') {
                        floor.houses.forEach(function(house) {
                            var qualityMeasure = self.qualityMeasureMap[house.id];
                            if(qualityMeasure != null) {
                                if(qualityMeasure['建设单位'] != null) {
                                    measure++;
                                }
                            }
                        });
                    } else {
                    	var qualityMeasure = self.qualityMeasureMap[self.unitId + '_' + floor.id];
                        if(qualityMeasure != null) {
                            if(qualityMeasure['建设单位'] != null) {
                                measure++;
                            }
                        }
                    }
        		});
        		var result = measure / total * 100;
        		result = result.toFixed(1);
        		return result;
        	}
        },
        measureCountAll: function() {
        	var total = 0;
        	var checkItemGuideline = this.$store.state.global.checkItemGuidelineMap[this.problemClassId];
        	this.floors.forEach(function(floor) {
        		if(checkItemGuideline == null || checkItemGuideline.process_mode == '1') {
                    floor.houses.forEach(function(house) {
                        total++;
                    });
        		} else {
        			total++;
        		}
        	});
        	return total;
        }
    },
    /* created() {
    	this.isAlert = true;
    	this.backMessage = '工作台-实测实量';
    }, */
	
}
</script>